import pathlib
from typing import List
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    """应用全局配置类"""

    # 基本配置
    DEBUG: bool = True
    ENV: str = "development"

    TITLE: str = "FastGraph API"
    DESC: str = "基于FastAPI和LangGraph的智能体"
    VERSION: str = "1.0.0"
    LOG_LEVEL: str = "info"

    HOST: str = "127.0.0.1"
    PORT: int = 8001

    # 项目路径
    PROJECT_PATH: pathlib.Path = pathlib.Path(__file__).parent.parent
    LOG_PATH: pathlib.Path = PROJECT_PATH / "logs"
    STATIC_PATH: pathlib.Path = PROJECT_PATH / "static"

    # 是否启用静态文件
    STATIC_FILE: bool = True

    # CORS 配置
    CORS_ORIGINS: List[str] = ["*"]

    # PostgreSQL 配置
    POSTGRES_HOST: str = "172.19.177.2"
    POSTGRES_PORT: int = 5432
    POSTGRES_USER: str = "postgres"
    POSTGRES_PASSWORD: str = "Hec2025pg."
    POSTGRES_DB: str = "llm"
    POSTGRES_MIN_SIZE: int = 1
    POSTGRES_MAX_SIZE: int = 20
    POSTGRES_TIMEOUT: int = 30

    # Redis 配置
    REDIS_HOST: str = "172.19.177.2"
    REDIS_PORT: int = 6379
    REDIS_PASSWORD: str = "Hec2025redis."
    REDIS_DB: int = 4
    REDIS_MAX_CONNECTIONS: int = 20
    REDIS_TIMEOUT: int = 30

    
    # Qwen 模型配置
    QWEN30B_MODEL: str = "./qwen3-32b"
    QWEN30B_BASE_URL: str = "http://172.16.53.17:51090/v1"
    QWEN30B_API_KEY: str = "sk-xxx"

    QWEN235B_MODEL: str = "qwen3"
    QWEN235B_BASE_URL: str = "http://xai.dyg.com.cn/v1"
    QWEN235B_API_KEY: str = "dyg-y2fyntVOODsN7a1iNUgqjfh6dcBMfpciZWhn4KGJ1x8"

    # Tavily 与 Google 模型配置
    TAVILY_API_KEY: str = "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"

    
    # 安全配置
    SECRET_KEY: str = "super-secret-key"

    # 内部开关
    INIT_START: bool = True  # 是否初始化 checkpointer

    # ========== 动态属性 ==========
    @property
    def DB_URI(self) -> str:
        return (
            f"postgresql://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}"
            f"@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}?sslmode=disable"
        )

    @property
    def REDIS_URI(self) -> str:
        return (
            f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"
            f"?password={self.REDIS_PASSWORD}"
        )

    @property
    def is_development(self) -> bool:
        """是否为开发环境"""
        return self.ENV.lower() == "development"

    @property
    def is_production(self) -> bool:
        """是否为生产环境"""
        return self.ENV.lower() == "production"

    # Pydantic 环境配置
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=True,
        extra="ignore",
    )


# 全局配置实例
settings = Settings()


if __name__ == "__main__":
    print("==== 应用配置 ====")
    print(f"环境: {settings.ENV}")
    print(f"主机: {settings.HOST}:{settings.PORT}")
    print(f"数据库: {settings.DB_URI}")
    print(f"Redis: {settings.REDIS_URI}")
    print(f"是否开发环境: {settings.ENV}")
